package com.ht.medicaltreatment.activity

import android.animation.ValueAnimator
import android.content.Intent
import android.view.View
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.Lifecycle
import com.githang.statusbar.StatusBarCompat

import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.utils.SoundUtils
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_search_layout.iconRadar
import kotlinx.android.synthetic.main.activity_search_layout.imgWhitePoint
import kotlinx.android.synthetic.main.activity_search_layout.tvContent
import kotlinx.android.synthetic.main.activity_search_layout.tvName
import kotlinx.android.synthetic.main.activity_search_layout.tvSearch
import kotlinx.android.synthetic.main.activity_search_layout.tvState

/**
 * 寻物
 */
class SearchThingsActivity : BaseActivity() {
    private val duration = 700L
    private var uid = ""
    private var isStart = false
    override fun getLayoutId() = R.layout.activity_search_layout

    override fun init() {
        StatusBarCompat.setStatusBarColor(this,getColor(R.color.blueDeep))
        ScanUtils.init(false)
        val rotateAnimation = RotateAnimation(
            0f,45f,
            Animation.RELATIVE_TO_SELF,
            1f,
            Animation.RELATIVE_TO_SELF,
            1f
        )
        rotateAnimation.duration = duration
        rotateAnimation.fillAfter = true
        rotateAnimation.repeatMode = ValueAnimator.REVERSE
        rotateAnimation.repeatCount = -1
        rotateAnimation.interpolator = LinearInterpolator()
        iconRadar.startAnimation(rotateAnimation)

        val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            uid = it.data?.getStringExtra("uid")?:""
            tvName.text = it.data?.getStringExtra("name")
            tvContent.text = it.data?.getStringExtra("name")
        }

        tvContent.setOnClickListener {
            launcher.launch(Intent(this,SearchResultActivity::class.java))
        }

        tvSearch.setOnClickListener {
            startSearchThings()
        }
    }

    private fun startSearchThings() {
        if(uid == ""){
            showToast("请输入您要搜索的物品名称")
            return
        }
        if(isStart){
            ScanUtils.close()
            tvSearch.text = "开始搜索"
            isStart = false
            tvSearch.setBackgroundResource(R.drawable.bg_search_btn)
        }else{
            isStart = true
            tvSearch.text = "停止搜索"
            tvSearch.setBackgroundResource(R.drawable.shape_search_things_yellow_bg)
            ScanUtils.open {
                if(it == uid){
                    SoundUtils.playSound(SoundUtils.SOUND_SUCCESS)
                    if(imgWhitePoint.visibility != View.VISIBLE){
                        imgWhitePoint.visibility = View.VISIBLE
                        tvState.text = "搜索到了"
                    }
                }else{
                    if(imgWhitePoint.visibility != View.GONE){
                        imgWhitePoint.visibility = View.GONE
                        tvState.text = ""
                    }
                }
            }
        }
    }

    override fun onKeyDown() {
        if(lifecycle.currentState == Lifecycle.State.RESUMED){
            startSearchThings()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ScanUtils.onDestroy()
    }
}